package ezXpns.storage.queries;

import java.util.regex.Pattern;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.WildcardQuery;

import ezXpns.BudgetChange;
import ezXpns.storage.Query;


//@author A0088827L
public class NameQuery implements Query<BudgetChange> {
	public NameQuery(String name) {
		String regex = name.replaceAll("\\*", ".*");
		this.pattern = Pattern.compile(regex);//compile for efficiency
		this.luceneQuery = new WildcardQuery(new Term("name", name));
	}

	@Override
	public boolean apply(BudgetChange arg) {
		return pattern.matcher(arg.getName()).matches();
	}

	@Override
	public org.apache.lucene.search.Query asLuceneQuery() {
		return luceneQuery;
	}
	
	Pattern pattern;
	org.apache.lucene.search.Query luceneQuery;
}
